<?php
class News_Block_m extends MY_Model{
	
	public $_table_lang = 'news_categories_lang';
	function __construct(){
		parent::__construct();
		$this->_table = 'news_categories';		
	}
	
	//lay de quy
	function get_all( $parent_id = 0 , $sym = '' ){
		//$this->_table .=' c';
		//$this->_table_lang .= ' l';
		$this->_table = 'news_categories c';
		$this->_table_lang = 'news_categories_lang l';
		
		$this->db->select('c.id, c.created, c.ordering,c.parent_id, l.title, l.slug, l.status');
		$this->db->order_by('c.ordering');
		$this->db->join($this->_table_lang,' l.id =  c.id');
		$this->db->where('l.lang',$this->_lang);
		$this->db->where('c.parent_id',$parent_id);
		$result =  parent::get_all();
		foreach($result as $item){
			$item->frefix = $sym;
			$item->child = $this->get_all($item->id,$sym.'►');
		}
		
		return $result;
	}
	
	function block(){
		$params = func_get_args();
		$parent_id = $params[0]->parent_id;
		$class = ( $params[0]->_class ) ? $params[0]->_class : "";
		$html = "<ul class=\"$class\">";
		
		
		$data = $this->get_all($parent_id);
		foreach($data as $item){
			$item->child = $this->categories_child($item->id);
		}
		$html .= $this->render_cat($data);
		return $html.'</ul>';
	}
	function render_cat($data){
		$html = "";
		foreach($data  as $item){
				$hasChild = ($item->child) ? " class=\"hasChild\" " : "";
				$tag_s = ($item->child) ? "<ul>"  : "";
				$tag_e = ($item->child) ? "</ul>"  : "";
				$rel = "rel=\"".$item->id."\"";
				$html .= "<li $hasChild $rel ><a href=\"".base_url('news/categories/'.$item->slug.'')."\">".$item->title.'</a>';
				if($item->child)
					$html .= $tag_s.$this->render_cat($item->child).$tag_e;//render_cat_child
				$html .= "</li>";
		}
		return $html;
	}
	function categories_child($parent_id){
		$data = $this->get_all($parent_id);
		foreach($data as $item){
			$item->child = $this->categories_child($item->id);
		}
		return $data;
	}
}